package cn.tedu.demo.service.impl;



import cn.tedu.demo.ex.ServiceException;
import cn.tedu.demo.mapper.UserInfoMapper;
import cn.tedu.demo.mapper.UserMapper;
import cn.tedu.demo.pojo.dto.UserInfoDTO;
import cn.tedu.demo.pojo.entity.User;
import cn.tedu.demo.pojo.entity.UserInfo;
import cn.tedu.demo.pojo.vo.UserInfoVO;
import cn.tedu.demo.pojo.vo.UserVO;
import cn.tedu.demo.service.IUserInfoService;
import cn.tedu.demo.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
@Slf4j
@Service
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private UserMapper userMapper;


    @Override
    public UserInfoVO getInfoByUserId(HttpSession session) {
        //判断登录状态
        UserVO currentUser = (UserVO) session.getAttribute("user");
        if (currentUser==null){
            String message="请先登录!";
            throw new ServiceException(ServiceCode.ERROR_FORBIDDEN,message);
        }
        //通过Session获取当前的id
        Integer id=currentUser.getId();
        Integer userId=id;
        System.out.println("userId:"+userId);
        UserInfoVO uInfo = userInfoMapper.getInfoByUserId(userId);
        uInfo.setId(id);
        if (uInfo==null){
            String message="请先完善信息!";
            throw new ServiceException(ServiceCode.ERROR_FORBIDDEN,message);
        }
        log.debug("service返回对象uInfo:{}",uInfo);
        return uInfo;
    }


    @Override
    public void update(UserInfoDTO userInfoDTO, HttpSession session) {
        //获取当前用户id信息,并赋值给DTO对象的userId.
        UserVO currentUser=(UserVO) session.getAttribute("user");
        Integer id=currentUser.getId();
        Integer userId=id;
        userInfoDTO.setUserId(userId);
        //统计user_info 表中是否存在userId匹配的数据
        int count =userInfoMapper.countByUserId(userId);
        if(count==0){
            //数量为0,即将为用户完善用户信息

            //新建实体类userInfo,复制Dto信息到userInfo
            UserInfo userInfo=new UserInfo();
            BeanUtils.copyProperties(userInfoDTO,userInfo);
            //调用userInfoMapper新增数据
            int rows = userInfoMapper.insert(userInfo);
            if (rows != 1) {
                String message = "信息完善失败，服务器忙，请稍后再尝试！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERROR_INSERT, message);
            }
        }else if (count==1){
            //数量为1,即将为用户修改用户信息

            //修改user_info表
            UserInfo userInfo=new UserInfo();
            BeanUtils.copyProperties(userInfoDTO,userInfo);
            userInfoMapper.update(userInfo);
            //修改user表
            User user=new User();
            BeanUtils.copyProperties(userInfoDTO,user);
            user.setId(id);
            int rows= userMapper.update(user);
            log.debug("rows:"+rows);
//            if (rows != 1) {
//                String message = "信息修改失败，服务器忙，请稍后再尝试！";
//                log.warn(message);
//                throw new ServiceException(ServiceCode.ERROR_INSERT, message);
//            }
        }
    }
}
